#include "dlgformattersettings.h"
#include "astylepredefinedstyles.h"
#include <wx/radiobut.h>
#include <wx/checkbox.h>
#include <wx/combobox.h>
#include <wx/spinctrl.h>
#include <wx/xrc/xmlres.h>

DlgFormatterSettings::DlgFormatterSettings( wxWindow *dlg ) :
  m_dlg( dlg ) {
}

DlgFormatterSettings::~DlgFormatterSettings() {
}

void DlgFormatterSettings::ApplyTo( astyle::ASFormatter& formatter ) {
  formatter.setFormattingStyle( astyle::STYLE_JAVA );
  formatter.setAttachClass( XRCCTRL( *m_dlg, "chkAttachClasses", wxCheckBox )->GetValue() );
  formatter.setAttachExternC( XRCCTRL( *m_dlg, "chkAttachExternC", wxCheckBox )->GetValue() );
  formatter.setAttachNamespace( XRCCTRL( *m_dlg, "chkAttachNamespaces", wxCheckBox )->GetValue() );
  formatter.setAttachInline( XRCCTRL( *m_dlg, "chkAttachInlines", wxCheckBox )->GetValue() );
  int contNum = XRCCTRL( *m_dlg, "spnContinuation", wxSpinCtrl )->GetValue();
  if( contNum > 0 && contNum <= 4 ) {
    formatter.setContinuationIndentation( contNum );
  }
  formatter.setCaseIndent( XRCCTRL( *m_dlg, "chkIndentCase", wxCheckBox )->GetValue() );
  formatter.setClassIndent( true );
  formatter.setLabelIndent( true );
  formatter.setModifierIndent( true );
  formatter.setNamespaceIndent( true );
  formatter.setSwitchIndent( XRCCTRL( *m_dlg, "chkIndentSwitches", wxCheckBox )->GetValue() );
  formatter.setPreprocBlockIndent( XRCCTRL( *m_dlg, "chkIndentPreprocBlock", wxCheckBox )->GetValue() );
  formatter.setPreprocDefineIndent( true );
  formatter.setPreprocConditionalIndent( true );
  formatter.setIndentCol1CommentsMode( true );
  int minConditionalEvent = XRCCTRL( *m_dlg, "spnMinConditionalEvent", wxSpinCtrl )->GetValue();
  formatter.setMinConditionalIndentOption( minConditionalEvent );
  formatter.setMaxInStatementIndentLength( wxAtoi( XRCCTRL( *m_dlg, "txtMaxInStatementIndent", wxTextCtrl )->GetValue() ) );
  formatter.setBreakClosingHeaderBracketsMode( XRCCTRL( *m_dlg, "chkBreakClosing", wxCheckBox )->GetValue() );
  formatter.setAddBracketsMode( XRCCTRL( *m_dlg, "chkAddBrackets", wxCheckBox )->GetValue() );
  formatter.setRemoveBracketsMode( XRCCTRL( *m_dlg, "chkRemoveBrackets", wxCheckBox )->GetValue() );
  formatter.setBreakOneLineHeadersMode( !XRCCTRL( *m_dlg, "chkKeepHeaders", wxCheckBox )->GetValue() );
  formatter.setBreakOneLineStatementsMode( !XRCCTRL( *m_dlg, "chkKeepStatements", wxCheckBox )->GetValue() );
  formatter.setTabSpaceConversionMode( false );
  formatter.setCloseTemplatesMode( XRCCTRL( *m_dlg, "chkCloseTemplates", wxCheckBox )->GetValue() );
  if( XRCCTRL( *m_dlg, "chkBreakLines", wxCheckBox )->GetValue() ) {
    formatter.setMaxCodeLength( wxAtoi( XRCCTRL( *m_dlg, "txtMaxLineLength", wxTextCtrl )->GetValue() ) );
    formatter.setBreakAfterMode( XRCCTRL( *m_dlg, "chkBreakAfterLogical", wxCheckBox )->GetValue() );
  } else {
    formatter.setMaxCodeLength( INT_MAX );
  }
  formatter.setBreakBlocksMode( XRCCTRL( *m_dlg, "chkBreakBlocks", wxCheckBox )->GetValue() );
  formatter.setBreakClosingHeaderBlocksMode( XRCCTRL( *m_dlg, "chkBreakBlocksAll", wxCheckBox )->GetValue() );
  formatter.setOperatorPaddingMode( true );
  formatter.setParensFirstPaddingMode( XRCCTRL( *m_dlg, "chkPadFirstParenOut", wxCheckBox )->GetValue() );
  formatter.setParensHeaderPaddingMode( XRCCTRL( *m_dlg, "chkPadHeader", wxCheckBox )->GetValue() );
  formatter.setParensUnPaddingMode( true );
  formatter.setCommaPaddingMode( true );
  formatter.setDeleteEmptyLinesMode( XRCCTRL( *m_dlg, "chkDelEmptyLine", wxCheckBox )->GetValue() );
  formatter.setEmptyLineFill( XRCCTRL( *m_dlg, "chkFillEmptyLines", wxCheckBox )->GetValue() );
  wxString pointerAlign = XRCCTRL( *m_dlg, "cmbPointerAlign", wxComboBox )->GetValue();
  if( pointerAlign == _T( "Type" ) ) {
    formatter.setPointerAlignment( astyle::PTR_ALIGN_TYPE );
  } else if( pointerAlign == _T( "Middle" ) ) {
    formatter.setPointerAlignment( astyle::PTR_ALIGN_MIDDLE );
  } else if( pointerAlign == _T( "Name" ) ) {
    formatter.setPointerAlignment( astyle::PTR_ALIGN_NAME );
  } else {
    formatter.setPointerAlignment( astyle::PTR_ALIGN_NONE );
  }
  wxString referenceAlign = XRCCTRL( *m_dlg, "cmbReferenceAlign", wxComboBox )->GetValue();
  if( referenceAlign == _T( "Type" ) ) {
    formatter.setReferenceAlignment( astyle::REF_ALIGN_TYPE );
  } else if( referenceAlign == _T( "Middle" ) ) {
    formatter.setReferenceAlignment( astyle::REF_ALIGN_MIDDLE );
  } else if( referenceAlign == _T( "Name" ) ) {
    formatter.setReferenceAlignment( astyle::REF_ALIGN_NAME );
  } else {
    formatter.setReferenceAlignment( astyle::REF_ALIGN_NONE );
  }
}
